﻿using Shinva.LADModel.Abstractions;
using Shinva.Variables.Interface;
using Shinva.Variables.Models;
using Shinva.Variables.Type.bit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shinva.LADModel.Type.WordLogicOperations
{
    public enum WordLogicOperation
    {
        And,
        Or,
        Xor
    }
    public class AndOrXor<T> : ElementNodeBase where T : IPlcMember
    {
        public T in1 { get; set; }
        public T in2 { get; set; }
        public T in3 { get; set; }
        public T in4 { get; set; }
        public T in5 { get; set; }
        public T in6 { get; set; }
        public T OUT { get; set; } //实为out

        private WordLogicOperation _logicOperation;
        private int _inCount = 2;
        public override PlcBool EN
        {
            get
            {
                return en;
            }
            set
            {
                en = value;
            }
        }
        public override PlcBool ENO
        {
            get
            {

                eno.Value = en.Value && LogicValue();
                return eno;
            }
            set
            {
                eno = value;
            }
        }

        public AndOrXor(int inCount, WordLogicOperation logicOperation)
        {
            Name = "AndOrXor";
            _inCount = inCount;
            _logicOperation = logicOperation;
            variableTableInterface.AddVariable("in1", StorageType.Input);
            variableTableInterface.AddVariable("in2", StorageType.Input);
            variableTableInterface.AddVariable("in3", StorageType.Input);
            variableTableInterface.AddVariable("in4", StorageType.Input);
            variableTableInterface.AddVariable("in5", StorageType.Input);
            variableTableInterface.AddVariable("in6", StorageType.Input);
            variableTableInterface.AddVariable("out", StorageType.Output);

            in1 = variableTableInterface.GetVariableValue<T>("in1");
            in2 = variableTableInterface.GetVariableValue<T>("in2");
            in3 = variableTableInterface.GetVariableValue<T>("in3");
            in4 = variableTableInterface.GetVariableValue<T>("in4");
            in5 = variableTableInterface.GetVariableValue<T>("in5");
            in6 = variableTableInterface.GetVariableValue<T>("in6");
            OUT = variableTableInterface.GetVariableValue<T>("out");
        }
        public override IPlcMember GetInterfaceVariableByName(string name)
        {
            if (name == "out") return OUT;
            else return variableTableInterface.GetVariableValue<IPlcMember>(name);
        }
        public bool LogicValue()
        {
            if (in1 == null || in2 == null)
            {
                return false;
            }
            switch (_logicOperation)
            {
                //case WordLogicOperation.And:
                //    return in1.Value && in2.Value;
                //case WordLogicOperation.Or:
                //    return in1.Value || in2.Value;
                //case WordLogicOperation.Xor:
                //    return in1.Value ^ in2.Value;
                default:
                    return false;
            }
        }
    }
}
